<template>
  <ContentWrap>
    <div class="mb-10px">
      <el-button
        type="primary"
        @click="openForm('create')"
        v-hasPermi="['oa:company-shareholders:create']"
      >
        <Icon icon="ep:plus" /> 新增股东
      </el-button>
    </div>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <!-- <el-table-column label="序号" align="center" prop="id" min-width="80" /> -->
      <el-table-column label="股东姓名" align="center" prop="shareholderName" min-width="150" />
      <el-table-column label="持股比例(%)" align="center" prop="shareholdingRatio" min-width="150" />
      <!-- <el-table-column label="创建者" align="center" prop="creatorName" min-width="120" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        min-width="200"
      /> -->
      <el-table-column label="操作" align="center" min-width="150" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['oa:accounting-companies:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['oa:accounting-companies:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <CompanyShareholdersForm ref="formRef" type="form" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { CompanyShareholdersApi, CompanyShareholdersVO } from '@/api/oa/companyshareholders/index'
import CompanyShareholdersForm from '@/views/oa/companyshareholders/CompanyShareholdersForm.vue'

/** 企业股东信息 列表 */
defineOptions({ name: 'ShareholderList' })

const props = defineProps<{
  companyId: number
  businessType: string // 业务类型：registered-company-注册企业，accounting-companies-记账企业
}>()

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<CompanyShareholdersVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  companyId: 0,
  businessType: ''
})
const formRef = ref() // 表单组件引用

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    queryParams.companyId = props.companyId
    queryParams.businessType = props.businessType
    const data = await CompanyShareholdersApi.getCompanyShareholdersPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 监听 companyId 变化 */
watch(() => props.companyId, (val, oldVal) => {
  console.log('ShareholderList - companyId changed:', { oldVal, newVal: val })
  if (val) {
    getList()
  } else {
    // 当 companyId 为空时，清空列表
    list.value = []
    total.value = 0
  }
}, { immediate: true }) // 添加 immediate: true，确保组件挂载时就执行一次

/** 打开股东表单 */
const openForm = (type: string, id?: number, data?: any) => {
  formRef.value?.open(type, id, {
    companyId: props.companyId,
    companyName: data?.companyName,
    businessType: props.businessType,
    readonly: type === 'detail'
  })
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await CompanyShareholdersApi.deleteCompanyShareholders(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

defineExpose({ getList, openForm }) // 提供 getList 和 openForm 方法
</script> 